// pages/home_new/home_new.js
const $api = require('../../utils/api').API.home
const $cache = require('../../utils/storage')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    schoolList:[],
    school:'',
    schoolIndex:0,
    tabIndex:1,
    tabs:['关注','发现','推荐'],
    careSubTabIndex:0,
    careSubTabs:['全部','学校','个人'],
    findSubTabIndex:0,
    findSubTabs:['随便看看','视频','世界杯','直播'],
    findList:[],
    focusList:[],
    params:{
      page:1,
      size:20,
      type:0,
      schoolId:10001
    },
  },
  bindPickerChange: function(e) {
    let index = e.detail.value
    let school = this.data.schoolList[index].schoolName
    $cache._setStorage('schoolName',school)
    console.log('picker发送选择改变，携带值为', this.data.schoolList[index])
    this.setData({
      schoolIndex: index,
      school
    })
    $api.getList({...this.data.params,schoolId:this.data.schoolList[index].id}).then(res=>{
      this.setData({
        findList:res.data.data.list,
      })
    })
  },
  goAsk(){
    wx.redirectTo({
      url: '/pages/ask/ask',
    })
  },
  goSendDynamics(){
    wx.redirectTo({
      url: '/pages/send_ dynamics_new/send_dynamics_new',
    })
  },
  handleTab(e){

    let index = e.target.dataset.index
    let openId = $cache._getStorage('openId')
    this.setData({
      tabIndex:index
    })
    if(index==0){
      $api.getFocusList({openId}).then(res=>{
        this.setData({
          focusList:res.data.data
        })
      })
    }
  },
  getMale(){
    $api.getList({...this.data.params,type:1}).then(res=>{
      this.setData({
        findList:res.data.data.list,
      })
    })
  },
  getFemale(){
    $api.getList({...this.data.params,type:2}).then(res=>{
      this.setData({
        findList:res.data.data.list,
      })
    })
  },
  handleCareSubTab(e){
    let index = e.target.dataset.index
    this.setData({
      careSubTabIndex:index
    })
  },
  handleFindSubTab(e){
    let index = e.target.dataset.index
    this.setData({
      findSubTabIndex:index
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options,999)
    let userInfo = $cache._getStorage('userInfo')
    const {schoolId,schoolName}= userInfo||{schoolId:100001,schoolName:''}
    if(schoolName){
      this.setData({school:schoolName})
    }else{
      $api.getSchoolList({page:1,size:10}).then(res=>{
        console.log(res.data.list)
        this.setData({
          schoolList:res.data.data.list
        })
      })
    }
    $api.getList({...this.data.params,schoolId}).then(res=>{
      this.setData({
        findList:res.data.data.list,
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    console.log(1)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})